import { lazy, useCallback, useState, useEffect } from 'react'
/* -------------------------------------------------------- Declare -- */
/* -------------------------------------------------------- Async Component -- */
const Input = lazy(()=>import('@antd/form/input'))
const Button = lazy(()=>import('@antd/button'))
/* -------------------------------------------------------- 回到顶部 -- */
const Index = ({ 
	name, 							/** @param {String}				# name */
	label,							/** @param {String}				# 标签名 */
	size='small', 					/** @param {String}				# 大小 */
	p,								/** @param {String}				#  */
	mode,							/** @param {String}				#  输入框类型*/
	file,
	//
	value,							/** @param {Number}				# 值 */
	disabled,						/** @param {Boolan}				# 禁用 */
	// 获取值
	onChange,						/** @param {Event}				# 有 Form 时值改变时执行 */
	onChanged,						/** @param {Event}				# 值改变时执行 */
	// 自定义扩展
	input = {},						/** @param {Object}				# input */
	btn = {},						/** @param {Object}				# btn */
	// button
	btnText = '选择',				/** @param {Object}				# 按钮文本 */
	api,
}, ref)=> {// eslint-disable-line
	const [loading, setLoading ] = useState()
	const [ model, setModel ] = useState(value)
	api = api ? api : ( file ? 'file/pick_file' : 'file/pick_folder' )
	input = { 
		disabled, label, name, size, 
		type:mode,
		p: p ? p : '请输入文件夹路径',
		value: model,
		...input
	}
	
	useEffect(()=> { 
		setModel( value )
	}, [ value ]) // eslint-disable-line
	
	const _onChange = value => {
		let result = name ? { model: {[name]: value} } : null
		onChange?.( value )
		onChanged?.({ value, label, ...result  })
	}
	
	const onClick = useCallback(() => {
		window.$http.post(null, api, { loading:false, onLoading: v => setLoading(v) }).then(d => {
			if( d ) {
				_onChange(d)
				setModel(d)
			}
		})
	},[ ]) // eslint-disable-line
	const onInput = v => {
		setModel(v)
		_onChange(v)
	}
	return (
		<div className='fx'>
			<Input onChange={onInput} {...input} />
			<Button label={btnText} className='ml10' size={size} onClick={ onClick } loading={loading} {...btn} />
		</div>
	)
}

export default Index